class MaterialsController < ApplicationController
  
  autocomplete :material,:code, :display_value => :full_name
  
  def index
    params[:key_word] = "%#{params[:key_words]}%"
    @materials = Material.where('code like :key_word or name like :key_word',params).visible.order('created_at asc')
  end
  
  def new
    @material = Material.new
  end
  
  def create
    @material = Material.new(params[:material])
    if @material.save
      redirect_to :action => :index
    else
      render :new
    end
  end
  
  def edit
    @material = Material.find(params[:id])
  end
  
  def update
    @material = Material.find(params[:id])
    if @material.update_attributes(params[:material])
      redirect_to :action => :index
    else
      render :edit
    end
  end
  
  def hide
    @material = Material.find(params[:id])
    @material.hidden!
    redirect_to :action => :index
  end

  def visible
    @material = Material.find(params[:id])
    @material.visible!
    redirect_to :action => :index
  end
end
